package it.rikesh.aip2011.client.component;

import it.rikesh.aip2011.client.BaseLayout;
import it.rikesh.aip2011.client.service.ActivityService;
import it.rikesh.aip2011.client.service.ActivityServiceAsync;
import it.rikesh.aip2011.shared.model.Activity;
import java.util.List;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;

/**
 * @author  sunita
 */
public class ActivityCategoryLayout extends LayoutContainer {
    private static ActivityServiceAsync activityService = GWT.create(ActivityService.class);

    private Text txtTitle;
    private Image[] img;
    private Text[] txtName;
    private String category;
    private FlexTable tableAct;
    
    public ActivityCategoryLayout(String category){
        this.category=category;
        activityService.getActivityByCategory(category, new AsyncCallback<List<Activity>>() {

            public void onFailure(Throwable caught) {
                
            }

            public void onSuccess(List<Activity> result) {
                init(result.size());
                getInfoActivities(result);
                
                addComponent(result.size()); 
            }
        });
               
    }
    
    /**
     * Questo metodo si occupa di inizializzare tutte le variabili necessari
     * Le proprietà del test, la dimensione dei caratteri etc.
     */
    public void init(int size){
        txtName=new Text[size];
        img=new Image[size];
        tableAct=new FlexTable();
        tableAct.setLayoutData(new CenterLayout());
        txtTitle=new Text(category.toUpperCase());
        txtTitle.setStyleAttribute("color", "red");
        txtTitle.setStyleAttribute("text-align", "center");
        txtTitle.setStyleAttribute("font-weight", "bold");
        txtTitle.setStyleAttribute("font-size", "28px");
        
        for(int i=0;i<size;i++){
            txtName[i]=new Text();
            txtName[i].setStyleAttribute("text-align", "center");
            txtName[i].setStyleAttribute("font-weight", "bold");
            txtName[i].setStyleAttribute("font-size", "35px");
            txtName[i].setStyleAttribute("font-family", "Rock Salt");
            img[i]=new Image();
            img[i].setHeight("60px");
            img[i].getElement().getStyle().setMarginLeft(100, Unit.PX);
        }
    }
    
    /**
     * Permettere di ottenere tutti dati nel datatstore relativo alle attività
     * @param id id del rifugio da visualizzare
     */
    private void getInfoActivities(List<Activity> list){
        for (int i=0;i<list.size();i++) {
            txtName[i].setText(list.get(i).getName());
            txtName[i].setStyleAttribute("text-decoration", "underline");
            txtName[i].setStyleAttribute("color", "blue");
            txtName[i].addListener(Events.OnClick, new Listener<BaseEvent>() {
                
                public void handleEvent(BaseEvent be) {
                    BaseLayout.changeContent(new ActivityLayout(((Text)be.getSource()).getText(),true));
                }
            });
            img[i].setUrl("/images/attivita/"+list.get(i).getImg());
            tableAct.setWidget(i, 0, img[i]);
            tableAct.setWidget(i, 1, txtName[i]);
        }
    }
    
    private void addComponent(int size){
        add(txtTitle);
        add(tableAct);
        layout();
    }
}
